<?php
namespace App\Services;

use App\Models\NewsClassModel;
use App\Models\NewsModel;
use App\Models\UserFavorModel;
use App\Models\UserModel;
use Framework\BaseService;

class NewsService extends BaseService
{
    public function cate_news()
    {
        $info = NewsClassModel::query()
            ->orderBy('sort','desc')
            ->select('id','class_name','class_img','status')
            ->where('status',1)
            ->get();
        return $info;
    }


    /**
     * 文章列表
     * @param $cate_id
     * @param $is_elite
     * @param $size
     * @param $page
     * @return \App\Models\_EmptyModel[]|\Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Query\Builder[]|\Illuminate\Support\Collection
     */
    public function newsList($cate_id, $is_elite, $size, $page)
    {

        $args = [
            'id',
            'title',
            'title_alias',
            'head_img',
            'author_id',
            'news_class_id_root',
            'news_class_id',
            'like_times',
            'share_times',
            'view_times',
            'collect_times',
            'tags',
//            'content',文章主题内容放到详情里展示
            'is_elite',
            'status',
            'sort',
            'source',
            'created_at',
            'updated_at',
        ];
        $query = NewsModel::query()->select($args)->where('status',1);
        if($cate_id){
            $query->where('news_class_id', $cate_id);
        }
        //是否精选
        if($is_elite){
            $query->where('is_elite', 1);
        }
        $info =  $query->forPage($page, $size)->orderBy('sort','desc')->get()->map(function ($item){
            if($item->source == 1){
                $item->name = "管理员";
            }
            if($item->source == 2){
                $item->name = UserModel::query()->where('id',$item->author_id)->value('nickname');
            }
            if(strstr($item->head_img, 'https://') == false){
                $item->head_img = 'https://'.config('filesystems.disks.qiniu.domains.default').$item->head_img.'?imageslim';
            }
            return $item;
        });

       return $info;
    }

    public function newsDetails($user_id,$news_id)
    {
        $info =  NewsModel::query()->where('id',$news_id)->first()->toArray();
        $info['is_guanzhu'] = UserFavorModel::query()->where(['user_id'=>$user_id,'type'=>1,'is_collect'=>1])->exists();
        $info['is_dianzan'] = UserFavorModel::query()->where(['user_id'=>$user_id,'type'=>5,'is_collect'=>3])->exists();
        $info['is_shoucang'] = UserFavorModel::query()->where(['user_id'=>$user_id,'type'=>5,'is_collect'=>2])->exists();
        NewsModel::query()->where('id',$news_id)->increment('view_times');
        return $info;
    }

    public function collectNews($user_id,$news_id,$type,$is_collect)
    {

        $where = ['user_id'=>$user_id,'type'=>$type,'is_collect'=>$is_collect,'origin_id'=>$news_id];
        $flg = UserFavorModel::query()->where($where)->first();
        if($flg){
            //取消点赞/收藏
            $flg->delete();

        }
        $model = new UserFavorModel();
        $model->user_id = $user_id;
        $model->type = $type;
        $model->is_collect = $is_collect;
        $model->origin_id = $news_id;
        $model->saveOrFail();
        return true;
    }
}
